<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="news.bll.topic.*"%>
<%@ page import="news.bll.news.*"%>
<%@ page import="news.entity.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'do_addnews.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%!
  		String newName(String name){
  		int position=name.indexOf(".");
  		String suffix=name.substring(position==0?0:position);
  		String nName=UUID.randomUUID()+suffix;
  		return nName;
  	}
  	%>
  
    <%
	    int ntid = 0;
	  	String ntitle = null;
	  	String nauthor = null;
	  	String nsummary = null;
	  	String ncontent = null;
	  	String npicPath = null;
	  	
	  	DiskFileItemFactory factory = new DiskFileItemFactory();
    	ServletFileUpload upload = new ServletFileUpload(factory);
    	upload.setHeaderEncoding("utf-8");//防止图片中文名称乱码
    	List<FileItem> items = upload.parseRequest(request);
    	String picPath=application.getRealPath("images");
    	for(FileItem f: items){
    		if(f.isFormField()){
    			String fieldName = f.getFieldName();
    			if(fieldName.equals("ntid")){
    				ntid = Integer.parseInt(f.getString("utf-8"));
    			}else if(fieldName.equals("ntitle")){
    				ntitle = f.getString("utf-8");
    			}else if(fieldName.equals("nauthor")){
    				nauthor = f.getString("utf-8");
    			}else if(fieldName.equals("nsummary")){
    				nsummary = f.getString("utf-8");
    			}else if(fieldName.equals("ncontent")){
    				ncontent = f.getString("utf-8");
    			}
    		}else{
    			if(f.getName()==""){
    				continue;
    			}
    			String newName=newName(f.getName());
    			f.write(new File(picPath,newName));
    			npicPath = "images/"+newName;
    		}
    	}
    	
    	NewsDaoImpl ndi = new NewsDaoImpl();
    	Date date = new Date();
    	News n = new News(ntid,ntitle,nauthor,date,npicPath,ncontent,date,nsummary);
    	int result = ndi.addANews(n);
    	if(result>0){
    		%>
    		<script>
    			alert("新闻添加成功！");
    			window.location="admin.jsp";
    		</script>    		
    		<%
    	}else{
    		%>
    		<script>
    			alert("新闻添加失败！");
    			window.location="admin.jsp";
    		</script>    		
    		<%
    	}
    %>
  </body>
</html>
